package com.xp.util;

import org.apache.commons.lang3.StringUtils;

public class CronChineseTranslator {

    public static void main(String[] args) {
//        System.out.println(translateToChinese("0 18 16 5 * ?"));
        int h = 2;

//        0100000
        
        System.out.println(h << 4);
    }

    public static String translateToChinese(String cron) {
        if (StringUtils.isBlank(cron)) {
            throw new IllegalArgumentException("Cron表达式为空");
        }

        String[] parts = cron.split("\\s+");
        if (parts.length < 6 || parts.length > 7) {
            throw new IllegalArgumentException("Cron格式错误，需6或7个字段");
        }

        StringBuilder result = new StringBuilder();
        String second = parseField(parts[0], "秒", "每秒");
        String minute = parseField(parts[1], "分", "每分");
        String hour = parseField(parts[2], "点", "每小时");
        String day = parseDay(parts[3]);
        String month = parseField(parts[4], "月", "每月");
        String week = parseWeek(parts[5]);
        String year = (parts.length == 7) ? parseField(parts[6], "年", "每年") : "";

        // 组合逻辑（处理互斥字段）
        if (!day.isEmpty() && !week.isEmpty()) {
            throw new IllegalArgumentException("日和周字段不能同时指定");
        }

        result.append(year).append(month);
        if (!week.isEmpty()) result.append(week);
        result.append(day).append(hour).append(minute).append(second);
        return result.toString().replaceAll("(每[年月日点分秒])+", "每") + "执行一次";
    }

    private static String parseField(String field, String unit, String wildcard) {
        if (field.equals("*") || field.equals("?")) return wildcard;
        if (field.contains("/")) {
            String[] split = field.split("/");
            return String.format("从%s%s开始，每%s%s", split[0], unit, split[1], unit);
        }
        return field + unit;
    }

    private static String parseDay(String day) {
        if (day.equals("?") || day.equals("*")) return "";
        if (day.contains("L")) return "最后一天";
        if (day.contains("W")) return "最近工作日";
        return "第" + day + "天";
    }

    private static String parseWeek(String week) {
        if (week.equals("*") || week.equals("?")) return "";
        String[] days = week.split(",");
        StringBuilder sb = new StringBuilder("每周");
        for (String day : days) {
            switch (day) {
                case "1": case "SUN": sb.append("日"); break;
                case "2": case "MON": sb.append("一"); break;
                case "3": case "TUE": sb.append("二"); break;
                case "4": case "WED": sb.append("三"); break;
                case "5": case "THU": sb.append("四"); break;
                case "6": case "FRI": sb.append("五"); break;
                case "7": case "SAT": sb.append("六"); break;
                case "L": sb.append("最后一周"); break;
                default: sb.append(day);
            }
        }
        return sb.toString();
    }
}